//
//  LLIndividualSpellGroupViewController.m
//  LoveLove
//
//  Created by wkf on 2019/3/29.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLIndividualSpellGroupViewController.h"
#import "LLIndividualSpellGroupListViewController.h"

@interface LLIndividualSpellGroupViewController ()<UIScrollViewDelegate,UINavigationControllerDelegate>
@property (copy, nonatomic) NSArray *titlesArr;//标题数组
@property (strong, nonatomic) NSMutableArray *titleBtns;//标题按钮数组
@property (strong, nonatomic) NSMutableArray *attentionChildVCs;//子控制器
@property (strong, nonatomic) LLButton *selectedButton;//选择的按钮
@property (strong, nonatomic) UIScrollView *scrollView;//UIScrollView
@property (strong, nonatomic) UIImageView *lineImage;//下划线
@property (strong, nonatomic) UIView *titlesView;//标题文本

@end

@implementation LLIndividualSpellGroupViewController

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
    
    self.titlesArr = @[@"拼团商品",@"我的拼团"];
    
    _attentionChildVCs = [NSMutableArray array];
    for (NSInteger i = 0; i < self.titlesArr.count; i++) {
        LLIndividualSpellGroupListViewController *vc = [LLIndividualSpellGroupListViewController new];
        [_attentionChildVCs addObject:vc];
    }
    
    [self changeUserIdentityStatus];
    [self setUpChildViewController];
    [self setUpScrollView];
    [self setUpTitlesView];
    [self addChildVcView];
    
}

#pragma mark - 切换身份
- (void)changeUserIdentityStatus {
    // 移除subView和childViewControllers
    [self.titlesView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat buttonW = LLScreenWidth / self.titlesArr.count;
    CGFloat buttonH = self.titlesView.height;
    self.scrollView.contentSize = CGSizeMake(LLScreenWidth * self.titlesArr.count, 0);
    for (int i= 0; i < self.titlesArr.count ; i ++) {
        LLButton *titleButton = self.titleBtns[i];
        [titleButton setTitle:self.titlesArr[i] forState:0];
        titleButton.tag = i;
        titleButton.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView addSubview:titleButton];
        LLIndividualSpellGroupListViewController *vc = self.attentionChildVCs[i];
        vc.type = i;
        [self addChildViewController:vc];
    }
    
    LLIndividualSpellGroupListViewController *childVc = self.childViewControllers[0];
    childVc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVc.view];
    [self titleClick:self.titleBtns[0]];
}

#pragma mark - 设置标题view
-(void)setUpTitlesView {
    [self titleView];
    self.titleView.backgroundColor = LLColorRGB(243);
    self.titleLabel.text = @"点赞历史";
    [self.rightButton setImage:[UIImage imageNamed:@"search_128px_1223096_easyicon.net 拷贝 2"] forState:0];
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navH, LLScreenWidth, 40)];
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    self.titlesView = titleView;
    
    UIView *lineview123 = [[UIView alloc] initWithFrame:CGRectMake(0, 39+self.navH, LLScreenWidth, 1)];
    lineview123.backgroundColor = LLColorRGB(235);
    [self.view addSubview:lineview123];
    
    NSArray *titlesArr = self.titlesArr;
    CGFloat buttonW = LLScreenWidth / titlesArr.count;
    CGFloat buttonH = titleView.height;
    for (int i= 0; i < titlesArr.count ; i ++) {
        LLButton *titleButton = self.titleBtns[i];
        [titleButton setTitle:titlesArr[i] forState:0];
        titleButton.tag = i;
        titleButton.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleButton];
    }
    LLButton *button = titleView.subviews.firstObject;
    [button.titleLabel sizeToFit];
    button.selected = YES;
    self.selectedButton = button;
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(titleView.width/self.titlesArr.count/2-50/2, titleView.height - 3, 50, 3)];
    imageview.image = [UIImage imageNamed:@"矩形 4 拷贝 5"];
    [self.titlesView addSubview:imageview];
    self.lineImage = imageview;
}

#pragma mark - 设置scrollView
-(void)setUpScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navH+40, LLScreenWidth, LLScreenHeight-40-self.navH)];
    scrollView.backgroundColor = LLColorRGB(243);
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    NSInteger count = [self.childViewControllers count];
    scrollView.contentSize = CGSizeMake(self.view.width * count, 0);
    scrollView.delegate = self;
    self.scrollView = scrollView;
}

-(void)setUpChildViewController {
    for (NSInteger i = 0; i < _attentionChildVCs.count; i++) {
        LLIndividualSpellGroupListViewController *vc = _attentionChildVCs[i];
        vc.type = i;
        [self addChildViewController:vc];
    }
}

#pragma mark - 添加子控制器
-(void)addChildVcView {
    int index = self.scrollView.contentOffset.x / self.scrollView.width;
    LLIndividualSpellGroupListViewController *childVc = self.childViewControllers[index];
    childVc.view.frame = self.scrollView.bounds;
    childVc.type = index;
    [self.scrollView addSubview:childVc.view];
    
}

#pragma mark ------------ UIScrollViewDelegate
// 点击titleButton滑动结束时，一定要调用[setcontentoffset animated ] 或者 [scrollerRactVisible animaated]方法让scroll产生滚动动画，动画结束时才会调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVcView];
    [LLNotificationCenter postNotificationName:@"EndScrollingAnimationNoti" object:nil];
}

#pragma mark - 滑动scrollview后减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 选中 点击对应的按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    // 添加子控制器
    [self addChildVcView];
    LLButton *button = self.titlesView.subviews[index];
    [self titleClick:button];
    [LLNotificationCenter postNotificationName:@"EndScrollingAnimationNoti" object:nil];
}

#pragma mark - 标题button点击事件
- (void)titleClick:(LLButton *)button {
    // 某个标题按钮被重复点击了
    if (button == self.selectedButton) {
        [LLNotificationCenter postNotificationName:@"TitleButtonDidRepeatClickNotification" object:nil];
    }
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineImage.width = button.titleLabel.width ;
        self.lineImage.centerX = button.centerX;
    }];
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = button.tag * self.view.width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark ------ 懒加载
- (NSMutableArray *)titleBtns {
    if (_titleBtns == nil) {
        _titleBtns = [NSMutableArray array];
        for (NSInteger i = 0; i < self.titlesArr.count; i++) {
            LLButton *btn = [LLButton new];
            [btn setTitleColor:LLColorRGB(0) forState:UIControlStateSelected];
            [btn setTitleColor:LLColorRGB(67) forState:0];
            btn.titleLabel.font = LLRegularFont(14);
            [_titleBtns addObject:btn];
        }
    }
    return _titleBtns;
}

#pragma mark ---- dealloc
- (void)dealloc {
    [LLNotificationCenter removeObserver:self];
}


#pragma mark - 搜索动作点击事件
-(void)didButtonWithrightButton{
    
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
